package com.initial.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.initial.entity.*;
import com.initial.mapper.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Business {

    @Resource
    private TbCrmBusinessMapper tbCrmBusinessMapper;
    @Resource
    private TbSystemUserMapper tbSystemUserMapper;
    @Resource
    private TbCrmCustomerMapper tbCrmCustomerMapper;
    @Resource
    private TbCrmContactsMapper tbCrmContactsMapper;
    @Resource
    private TbCrmBusinessStatusMapper tbCrmBusinessStatusMapper;

    //去商机模块
    @RequestMapping("/toBusiness")
    public String toBusiness() {
        return "views/manage/business/index_business";
    }

    //去商机页面
    @RequestMapping("/toListBusiness")
    public String toListBusiness() {
        return "views/manage/business/list_business";
    }

    //展示商机数据
    @RequestMapping("/listBusiness")
    @ResponseBody
    public Map listBusiness(Integer page, Integer rows, HttpSession session) {
        Map map = new HashMap();
        String types = (String) session.getAttribute("types");//获取存在session的type（day、week、month）
        if (types != null) {//如果不为空 执行方法
            IPage<TbCrmBusiness> times = tbCrmBusinessMapper.createTimes(types, new Page<>(page, rows));
            System.out.println(times.getTotal());
            System.out.println(times.getRecords());
            session.removeAttribute("types");
            map.put("total", times.getTotal());//查出来的总数量
            map.put("rows", times.getRecords());//查出来的总数据
            return map;//返回出去
        }
        String businessSearch = (String) session.getAttribute("businessSearch");//获取session值
        String keywords = (String) session.getAttribute("keywords");
        String custName = null;
        String name = null;
        String origin = null;
        String nextstep = null;
        //判断查询条件为哪个  然后赋值
        if ("custName".equals(businessSearch)) {
            custName = keywords;
        }
        if ("name".equals(businessSearch)) {
            name = keywords;
        }
        if ("origin".equals(businessSearch)) {
            origin = keywords;
        }
        if ("nextstep".equals(businessSearch)) {
            nextstep = keywords;
        }
        IPage tbCrmBusinessIPage = tbCrmBusinessMapper.selectBusiness(custName, name, origin, nextstep, new Page<>(page, rows));
        map.put("total", tbCrmBusinessIPage.getTotal());
        map.put("rows", tbCrmBusinessIPage.getRecords());
        session.removeAttribute("businessSearch");//清除session
        session.removeAttribute("keywords");
        return map;
    }

    //模糊
    @RequestMapping("/selectQuery")
    public String likeCustomer(String businessSearch, String keywords, HttpSession session) {
        session.setAttribute("businessSearch", businessSearch);//查询条件的name放入session
        session.setAttribute("keywords", keywords);//搜索按钮的name放入session
        return "views/manage/business/list_business"; //再次打回list页面
    }

    //查看商机
    @RequestMapping("/seeBusiness")
    public String seeBusiness(Integer id, HttpSession session, ModelMap modelMap) {
//        ModelMap modelMap = new ModelMap();  //创建对象不行 ，要写在形参里
        TbCrmBusiness tbCrmBusiness = tbCrmBusinessMapper.selectById(id);
        TbSystemUser tbSystemUser = tbSystemUserMapper.selectById(tbCrmBusiness.getOwnerUserId());
//        System.out.println(tbSystemUser.getUsername());
        TbCrmCustomer tbCrmCustomer = tbCrmCustomerMapper.selectById(id);     //根据id查出需要的 各个表中的数据
        TbCrmContacts tbCrmContacts = tbCrmContactsMapper.selectById(id);
        TbCrmBusinessStatus tbCrmBusinessStatus = tbCrmBusinessStatusMapper.selectById(tbCrmBusiness.getStatusId());
        modelMap.addAttribute("busiVO", tbCrmBusiness);
        modelMap.addAttribute("userVO", tbSystemUser);
        modelMap.addAttribute("custVO", tbCrmCustomer);       //查出来的数据放入modelMap中
        modelMap.addAttribute("conVO", tbCrmContacts);
        modelMap.addAttribute("statusVO", tbCrmBusinessStatus);
        return "views/manage/business/see_business";
    }

    //去修改页面
    @RequestMapping("/toEditBusiness")
    public String toEditBusiness(Integer id, ModelMap modelMap) {
        System.out.println(id);
        TbCrmBusiness tbCrmBusiness = tbCrmBusinessMapper.selectById(id);
        modelMap.addAttribute("busiVO", tbCrmBusiness);
        List<TbSystemUser> tbSystemUsers = tbSystemUserMapper.selectList(new QueryWrapper<>());
        modelMap.addAttribute("users", tbSystemUsers);
        List<TbCrmCustomer> tbCrmCustomers = tbCrmCustomerMapper.selectNameAndId();
        modelMap.addAttribute("customers", tbCrmCustomers);
        List<TbCrmContacts> tbCrmContacts = tbCrmContactsMapper.selectList(new QueryWrapper<>());
        modelMap.addAttribute("contacts", tbCrmContacts);
        List<TbCrmBusinessStatus> tbCrmBusinessStatuses = tbCrmBusinessStatusMapper.selectList(new QueryWrapper<>());
        modelMap.addAttribute("statuses", tbCrmBusinessStatuses);
        return "views/manage/business/edit_business";
    }

    /*//回显数据到编辑界面
    @RequestMapping("/echoEditBusiness")
    @ResponseBody
    public Map echoEditBusiness(Integer id) {
        System.out.println(id);
        Map map = new HashMap();
        List<TbSystemUser> tbSystemUsers = tbSystemUserMapper.selectList(new QueryWrapper<>());
        map.put("data", tbSystemUsers);
        return map;
    }*/

    //编辑商机
    @RequestMapping("/editBusiness")
    @ResponseBody
    public Map editBusiness(Integer id, TbCrmBusiness tbCrmBusiness) {
        Map map = new HashMap();
        System.out.println(id);
        int i = tbCrmBusinessMapper.updateById(tbCrmBusiness);
        if (i == 1) {
            map.put("success", true);
            map.put("msg", "修改成功");
        } else {
            map.put("success", false);
            map.put("msg", "修改失败");
        }
        return map;
    }

    //删除商机
    @RequestMapping("/remove")
    public String remove(Integer id) {
        System.out.println(id);
        tbCrmBusinessMapper.deleteById(id);
        return "forward:/toListBusiness";
    }


    //去添加商机页面
    @RequestMapping("/toAddBusiness")
    public String toAddBusiness(ModelMap modelMap) {
        List<TbSystemUser> tbSystemUsers = tbSystemUserMapper.selectList(new QueryWrapper<>());
        modelMap.addAttribute("users", tbSystemUsers);
        List<TbCrmCustomer> tbCrmCustomers = tbCrmCustomerMapper.selectAll();
        modelMap.addAttribute("customers",tbCrmCustomers );
        List<TbCrmContacts> tbCrmContacts = tbCrmContactsMapper.selectList(new QueryWrapper<>());
        modelMap.addAttribute("contacts", tbCrmContacts);
        List<TbCrmBusinessStatus> tbCrmBusinessStatuses = tbCrmBusinessStatusMapper.selectList(new QueryWrapper<>());
        modelMap.addAttribute("statuses", tbCrmBusinessStatuses);
        return "views/manage/business/add_business";
    }

    //添加商机
    @RequestMapping("/addBusiness")
    @ResponseBody
    public Map addBusiness(TbCrmBusiness business) {
        Map map = new HashMap();
        int insert = tbCrmBusinessMapper.insert(business);
        if (insert == 1) {
            map.put("success", true);
            map.put("msg", "添加成功");
        } else {
            map.put("success", false);
            map.put("msg", "添加失败");
        }
        return map;
    }

    //本日、周、月新建
    @RequestMapping("/creatTimes")
    public String createTime(@RequestParam("types") String types, HttpSession session) {
        //从前端过来的type类型存入session  （day、week、month）
        session.setAttribute("types", types);
        return "forward:/toListBusiness"; //转发到去展示页面的控制器
    }
}
